import { PageTmpItem } from '../../models';

/**
 * 生成页面的数据模型
 * @param template 模板
 */
export function getDataModel(pageTmpItem: PageTmpItem) {
    const dataModel: {
        [key: string]: any
    } = {};
    const components = pageTmpItem.template.tmpComItems.map(item => item.component);
    components.forEach(element => {
        const bindField = element.bindField;
        if (bindField) {
            const defaultValue = element.defaultValue;
            const isHasProperty = Object.prototype.hasOwnProperty.call(dataModel, bindField);
            if (!isHasProperty || !dataModel[bindField]) {
                dataModel[bindField] = defaultValue;
            }
        }
    });
    return dataModel;
}
